package {
	
	import alternativa.engine3d.controllers.CameraController;
	import alternativa.engine3d.controllers.ObjectController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.utils.FPS;
	import alternativa.utils.KeyboardUtils;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class MicroMondes extends Sprite {
		
		private var DEBUG:Boolean = true;
		
		// La scène 3D
		private var scene:Scene3D;
		// La vue
		private var view:View;
		// La caméra
		private var camera:Camera3D;
		// Le gestionnaire de camera
		private var cameraController:CameraController;
		
		// Le Monde
		private var monde:Monde = new Monde();
		//private var env:Environment = new Environment();
		
		// Barre de chargement
		private var loadingInfo:TextField;
		
		
		public function MicroMondes() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.frameRate = 100;
			// Initialisation de la scène
			initScene();
			// Initialisation de l'interface
			initUI();
			// Début du chargement des ressources quand le monde est chargé
			monde.addEventListener(Event.COMPLETE, onLoadComplete);
			
			// Paramètrage des gestionnaires d'évènements
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			stage.addEventListener(Event.RESIZE, onResize);
			// Initialisation de la taille du conteneur
			onResize();
		}
		
		public function initScene():void {
			scene = new Scene3D();
			scene.root = new Object3D("root");
			
			//scene.root.addChild(env);
			
			camera = new Camera3D();
			camera.x = 20;
			camera.y = -100;
			camera.z = 20;
			scene.root.addChild(camera);
			
			view = new View(camera);
			addChild(view);
			
			cameraController = new CameraController(stage);
			
			cameraController.setDefaultBindings();
			cameraController.bindKey(KeyboardUtils.UP, ObjectController.ACTION_UP);
			cameraController.bindKey(KeyboardUtils.DOWN, ObjectController.ACTION_DOWN);
			cameraController.bindKey(KeyboardUtils.LEFT, ObjectController.ACTION_LEFT);
			cameraController.bindKey(KeyboardUtils.RIGHT, ObjectController.ACTION_RIGHT);
			
			cameraController.camera = camera;
			cameraController.checkCollisions = true;
			cameraController.collisionRadius = 20;
			cameraController.controlsEnabled = true;
			
			cameraController.lookAt(monde.coords);
			
			scene.calculate();
		}
		
		public function initUI():void {
			// Champ texte avec une description des contrôles
			var info:TextField = new TextField();
			info.x = 3;
			info.y = 46;
			info.autoSize = TextFieldAutoSize.LEFT;
			info.selectable = false;
			info.text = "MicroMondes : Monde Full 3D v0.1\n";
			info.setTextFormat(new TextFormat("Tahoma", 10, 0xBB6633));
			info.defaultTextFormat = new TextFormat("Tahoma", 10, 0x333333); 
			/*info.appendText("\nDrag — look");
			info.appendText("\nWSAD or arrows — move");
			info.appendText("\nShift — run");
			info.appendText("\nSpace — jump");
			info.appendText("\nWheel or [ ] — change FOV");
			info.appendText("\n+/- — change screen size");
			info.appendText("\nT — show triangulation");*/
			addChild(info);
			
			// Status du chargement
			loadingInfo = new TextField();
			loadingInfo.x = 3;
			loadingInfo.text = "Chargement du monde";
			loadingInfo.autoSize = TextFieldAutoSize.LEFT;
			loadingInfo.selectable = false;
			loadingInfo.setTextFormat(new TextFormat("Tahoma", 10, 0xFF0000));
			loadingInfo.defaultTextFormat = new TextFormat("Tahoma", 10, 0xFF0000); 
			addChild(loadingInfo);
			
			if(DEBUG) FPS.init(stage);
		}
		
		private function onResize(e:Event = null):void {
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
		
		private function initCamera():void {
			// Initialisation de la caméra
			camera = new Camera3D();
			camera.x = 20;
			camera.y = -100;
			camera.z = 20;
			scene.root.addChild(camera);
			// Initialisation du controlleur
			cameraController = new CameraController(stage);
			cameraController.camera = camera;
			cameraController.setDefaultBindings();
			cameraController.bindKey(KeyboardUtils.UP, ObjectController.ACTION_UP);
			cameraController.bindKey(KeyboardUtils.DOWN, ObjectController.ACTION_DOWN);
			cameraController.bindKey(KeyboardUtils.LEFT, ObjectController.ACTION_LEFT);
			cameraController.bindKey(KeyboardUtils.RIGHT, ObjectController.ACTION_RIGHT);
			cameraController.checkCollisions = true;
			cameraController.collisionRadius = 20;
			cameraController.controlsEnabled = false;
		}
		
		private function onEnterFrame(e:Event):void {
			// traitement des entrées utilisateur
			monde.rotationZ += 0.01;
			cameraController.processInput();
			// Calcul de la scène
			scene.calculate();
		}
		
		private function onLoadComplete(e:Event):void {
			loadingInfo.text = "";
			scene.root.addChild(monde);
		}
	
	}
}
